/*
 * @lc app=leetcode.cn id=503 lang=javascript
 *
 * [503] 下一个更大元素 II
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var nextGreaterElements = function(nums) {
  const newNums = nums.concat(nums);

  let stack = [];
  let result = new Array(nums.length * 2).fill(-1);
  for (let i = 0; i < newNums.length; i++) {
    while(stack.length && newNums[i] > newNums[stack[stack.length - 1]]) {
      result[stack.pop()] = newNums[i];
    }
    stack.push(i);
  }
  result.length = nums.length;
  return result;
};
// @lc code=end

